package com.flipdf2;

import java.awt.Toolkit;

public abstract class Length {

	public static final String POINT = "pt";
	public static final String INCH = "in";
	public static final String CM = "cm";
	protected int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();

	public static Length createLength(String length) {
		if (length.endsWith(Length.POINT)) {
			return new PointLength(length);
		}
		if (length.endsWith(Length.CM)) {
			return new CMLength(length);
		}
		return null;
	}

	public abstract int toScreenPixel();

	public static class PointLength extends Length {
		private double value;

		public PointLength(String length) {
			if (length.endsWith(POINT)) {
				String point = length.substring(0, length.length() - POINT.length());
				value = Double.parseDouble(point);
			}
		}

		public int toScreenPixel() {
			return (int) ((value / 72) * screenResolution);
		}
	}

	public static class CMLength extends Length {
		private double value;

		public CMLength(String length) {
			if (length.endsWith(CM)) {
				String point = length.substring(0, length.length() - CM.length());
				value = Double.parseDouble(point);
			}
		}

		public int toScreenPixel() {
			return (int) ((value / 2.54) * screenResolution);
		}
	}
}